$(function () {
    container.off('click');
    function getEquipmentRepertoriesByName(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "equipmentName":$("#searchName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getEquipmentRepertoriesByName(page);
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getEquipmentRepertoriesByName(0);
    });

    // 获取入库器材的界面
    $("#addEquipment").click(function() {
        $("#equipmentTitle").html("器材入库");
        axios({
            method:'get',
            url: "/equipment/storage"
        }).then(function (response) {
            $("#equipmentForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取器材借还界面
   container.on("click","#circulate",function () {
        $("#equipmentTitle").html("器材借/还");
        axios({
            method:'get',
            url:"/equipment/circulate/"+$(this).attr('equipmentId')
        }).then(function (response) {
            $("#equipmentForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取器材报修界面
   container.on("click","#repairs",function () {
        $("#equipmentTitle").html("器材报修");
        axios({
            method:'get',
            url:"/equipment/repairs/"+$(this).attr('equipmentId')
        }).then(function (response) {
            $("#equipmentForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取器材报废界面
    container.on("click","#scrapped",function () {
        $("#equipmentTitle").html("器材报废");
        axios({
            method:'get',
            url:"/equipment/scrapped/"+$(this).attr('equipmentId')
        }).then(function (response) {
            $("#equipmentForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //提交器材操作，清空表单
    $("#equipmentForm").submit(function() {
        var csrfToken = $("meta[name='_csrf']").attr("content");
        var title = $("#equipmentTitle").html();
        var url;
        switch(title){
            case "器材借/还":url ="/equipment/circulate"; break;
            case "器材入库":url ="/equipment/storage";break;
            case "器材报修":url ="/equipment/repairs";break;
            case "器材报废":url ="/equipment/scrapped";break;
        }
        axios({
            method:"POST",
            url:url,
            data:$('#equipmentForm').serialize(),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#equipmentForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getEquipmentRepertoriesByName(0);
                $("#equipmentForm").html("");
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}
        }).catch(function (error) {
            toastr.error("error!");
        });

        $("#equipmentDialogClose").click();
        return false;
    });

    //关闭器材借还界面
    $("#equipmentDialogClose").click(function () {
        $("#equipmentForm").html("");
    });

});